﻿using AutoMapper;
using Domain.Examin;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.ApprovalProcess;
using MES.Server.API.Dto.ApprovalProcess;
using ReturnValue;

namespace MES.Server.API.Application.Handler.ApprovalProcess
{
    public class GetConfigurationNodeHandler : IRequestHandler<GetConfigurationNodeCommand, ApiResult<List<ApprovalNodeDTO>>>
    {
        private readonly IRepository<ApprovalNodeModel> approvalNodeRepo;
        private readonly IRepository<PurchaseOrderModel> purchaseOrderRepo;

        public GetConfigurationNodeHandler(
            IRepository<ApprovalNodeModel> approvalNodeRepo,
            IRepository<PurchaseOrderModel> purchaseOrderRepo
            )
        {
            this.approvalNodeRepo = approvalNodeRepo;
            this.purchaseOrderRepo = purchaseOrderRepo;
        }
        public Task<ApiResult<List<ApprovalNodeDTO>>> Handle(GetConfigurationNodeCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<ApprovalNodeDTO>>();
            var quer = approvalNodeRepo.GetAll().Where(x => !x.IsDel);
            var pur = purchaseOrderRepo.GetAll().Where(x => !x.IsDel);

            pur = pur.Where(x => x.Id == request.processId);

            var linq = from a in quer
                       join b in pur on a.processId equals b.Id
                       select new ApprovalNodeDTO()
                       {
                           Id= a.Id,
                           processId = a.processId,
                           nodeName = a.nodeName,
                           nodeType = a.nodeType,
                           prevnodeId = a.prevnodeId,
                           nextnodeId = a.nextnodeId,
                           nodeDescription = a.nodeDescription,
                           timeLimit = a.timeLimit,
                       };
            
            res.Data = linq.ToList();
            res.Message = "查询成功";
            res.Code = StatusCode.Success;
            return Task.FromResult(res);
        }
    }
}
